<?

require_once(dirname(__FILE__).'/../models/Pages.php');
require_once(dirname(__FILE__).'/../models/PagesCategories.php');

class PagesController extends Main_Controller_Action {
	public function preDispatch(){
		
		if (!$this->loggedUser) {
        		die('You must be logged to manage pages');
        } else {
           
           /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
	
	function indexAction(){
		$this->view->pages = ModelMapper::instance('Pages')->findAll();
	}
	
	function addAction(){
		$filter = "WHERE sc_catalogue = 'general'";
		$orderBy = "ORDER BY status ASC";
		$statuses = ModelMapper::instance('Statuses')->findFiltered($filter, $orderBy);
		$this->view->statuses = $statuses;
		
		$filter = "";
		$orderBy = "ORDER BY category ASC, par_category ASC";
		$categories = ModelMapper::instance('PagesCategories')->findFiltered($filter, $orderBy);
		
		$this->view->editor2 = StringUtils::CKEDITOR('addForm[toplineText]', null, 'Basic', '98%', 3);
		$this->view->editor = StringUtils::CKEDITOR('addForm[content]', null);
		
		$this->view->categories = $categories;
        
		$smarty = $this->view->getEngine();
		$tpl = $smarty->fetch('pages/add.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function editAction(){
		//Database::instance()->getConn()->debug=1;
		$filter = "WHERE sc_catalogue = 'general'";
		$orderBy = "ORDER BY status ASC";
		$statuses = ModelMapper::instance('Statuses')->findFiltered($filter, $orderBy);
		$this->view->statuses = $statuses;
						
        $page = ModelMapper::instance('Pages')->find(intval($this->_request->getParam('pagesId')));
		
        $filter = "";
		$orderBy = "ORDER BY category ASC, par_category ASC";
		$categories = ModelMapper::instance('PagesCategories')->findFiltered($filter, $orderBy);
		$this->view->categories = $categories;	
                
		$this->view->editor2 = StringUtils::CKEDITOR('addForm[toplineText]', $page->getToplineText(), 'Basic', '98%', 3);
		$this->view->editor = StringUtils::CKEDITOR('addForm[content]', $page->getContent());
		
		$this->view->page = $page;
		
		$smarty = $this->view->getEngine();
		$tpl = $smarty->fetch('pages/edit.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function deleteAction(){
		//Database::instance()->getConn()->debug=1;
		$page = ModelMapper::instance('Pages')->find(intval($this->getRequest()->getParam('pagesId')));
        $page->delete();
        
        exit();
	}
	
	function saveAction(){
		Database::instance()->getConn()->debug=1;
		$formData = $this->_request->getParam('addForm');
		
		//debug($formData);
		//exit();

		if (intval($formData['pagesId']) >0){
			$obj = ModelMapper::instance('Pages')->find(intval($formData['pagesId']));
			$page = $obj->getPage();
		}
		else{
			$page = new Pages(null,null,false);

		}

			$page->setStatusesId(intval($formData['statusesId']));
			$page->setTitle(trim(strip_tags(stripcslashes($formData['title']))));
			$page->setTeaser(trim(strip_tags(stripcslashes($formData['teaser']))));
			$page->setContent(trim($formData['content']));
			$page->setMetaTitle(trim(strip_tags(stripcslashes($formData['metaTitle']))));
			$page->setMetaDescription(trim(strip_tags(stripcslashes($formData['metaDescription']))));
			$page->setMetaTags(trim(strip_tags(stripcslashes($formData['metaTags']))));
			$page->setUsersId(intval($this->loggedUser->getId()));
			$page->setToplineText(trim($formData['toplineText']));
			$page->setStaticUrl(trim($formData['staticUrl']));
			//$page->setPagesCategoriesId(intval($formData['pagesCategoriesId']));
			ObjectWatcher::flushOperations();

		//debug($artwork);

		/*if ($isNew == true){
			$shopCategoryMap = new ShopPagesCategoriesMap();
			$shopCategoryMap->setShopPagesCategoriesId(intval($formData['shopPagesCategoriesId']));
			$shopCategoryMap->setShopPagesId($obj->getId());
		} else {
			$category = ModelMapper::instance('ShopPagesCategoriesMap')->find(intval($formData['shopPagesCategoriesMapId']));
			$category->setShopPagesCategoriesId(intval($formData['shopPagesCategoriesId']));
		}*/
		exit();	
	}
}
	
?>